﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AspNetMvc.Demo1.Models;
using AspNetMvc.Demo1.Resources;

namespace AspNetMvc.Demo1.Controllers
{
    public class BookController : Controller
    {
        private SqlCeDbContext db = new SqlCeDbContext();

        //
        // GET: /Book/

        public ViewResult Index(string sortOrder)
        {
            #region ViewBag Resources

            ViewBag.Title = Resource1.BookIndexTitle;
            ViewBag.CreateLink = Resource1.CreateLink;
            ViewBag.DetailLink = Resource1.DetailLink;
            ViewBag.DeleteLink = Resource1.DeleteLink;
            ViewBag.EditLink = Resource1.EditLink;
            ViewBag.TitleDisplay = Resource1.TitleDisplay;
            ViewBag.IsbnDisplay = Resource1.IsbnDisplay;
            ViewBag.SummaryDisplay = Resource1.SummaryDisplay;
            ViewBag.AuthorDisplay = Resource1.AuthorDisplay;
            ViewBag.ThumbnailDisplay = Resource1.ThumbnailDisplay;
            ViewBag.PriceDisplay = Resource1.PriceDisplay;
            ViewBag.PublishedDisplay = Resource1.PublishedDisplay;
            #endregion

            #region ViewBag Sort Params
            // Define the sort orders - if the same link is 
            // clicked twice, reverse the direction from 
            // ascending to descending
            ViewBag.TitleSortParam = (sortOrder == "Title")
                ? "Title desc" : "Title";
            ViewBag.IsbnSortParam = (sortOrder == "Isbn")
                ? "Isbn desc" : "Isbn";

            ViewBag.AuthorSortParam = (sortOrder == "Author")
                ? "Author desc" : "Author";
            ViewBag.PriceSortParam = (sortOrder == "Price")
                ? "Price desc" : "Price";
            ViewBag.PublishedSortParam =(String.IsNullOrEmpty(sortOrder))
                ? "Published desc" : "";
            // Default the sort order
            if (String.IsNullOrEmpty(sortOrder))
            {
                sortOrder = "Published desc";
            }
            #endregion

            var books = db.Books;

            return View(books.ToList());
        }

      

        //
        // GET: /Book/Details/5

        public ViewResult Details(int id)
        {
            Book book = db.Books.Find(id);
            return View(book);
        }

        //
        // GET: /Book/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Book/Create

        [HttpPost]
        public ActionResult Create(Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(book);
        }

        //
        // GET: /Book/Edit/5

        public ActionResult Edit(int id)
        {
            Book book = db.Books.Find(id);
            return View(book);
        }

        //
        // POST: /Book/Edit/5

        [HttpPost]
        public ActionResult Edit(Book book)
        {
            if (ModelState.IsValid)
            {
                db.Entry(book).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(book);
        }

        //
        // GET: /Book/Delete/5

        public ActionResult Delete(int id)
        {
            Book book = db.Books.Find(id);
            return View(book);
        }

        //
        // POST: /Book/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Book book = db.Books.Find(id);
            db.Books.Remove(book);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}